package com.archerdu.apply.controller.admin;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.archerdu.apply.model.Product;
import com.archerdu.apply.model.ProductCatalog;
import com.archerdu.apply.model.User;
import com.archerdu.apply.service.ProductCatalogManager;
import com.archerdu.apply.service.ProductManager;
import com.archerdu.base.controller.BaseController;
import com.archerdu.base.dao.IPage;
import com.archerdu.commons.constant.DataConstant;
import com.archerdu.commons.constant.ErrorConstant;

@Controller
@Scope("prototype")
@RequestMapping("/admin/product")
public class ProductController extends BaseController {

    private static final long     serialVersionUID = 3298072203679257178L;

    private ProductCatalogManager productCatalogManager;
    private ProductCatalog        productCatalog;
    private ProductManager        productManager;
    private Product               product;

    @RequestMapping("/main")
    public String main() {
        return "admin/product/main";
    }

    @RequestMapping(value = "/add")
    public ModelAndView add(HttpServletRequest requ, ProductController mc) {
        ModelAndView mv = new ModelAndView();
        // 没有目录不能添加文章
        if (mc.getProduct() == null || null == mc.getProduct().getCatalogId()
                || null == productCatalogManager.get(mc.getProduct().getCatalogId())) {
            this.addMessage(ErrorConstant.ARTICLE_CATE_NOT_NULL);
            mv.setViewName(DataConstant.COMMEN_ERROR_PAGE);
            
        } else {
            product = mc.getProduct();

            mv.setViewName("admin/product/add");

            if ("get".equalsIgnoreCase(requ.getMethod())) {
                mv.addObject(product);
            } else {
                try {
                    product.setCreateUser(((User) requ.getSession().getAttribute(DataConstant.SESSION_SYS_USER))
                            .getId());
                    product.setCreatetime(getSqlDate());
                    productManager.add(product);
                    addMessage(DataConstant.COMMON_ADD_SUCCESS);
                } catch (Exception e) {
                    addMessage(DataConstant.COMMON_ADD_FAILED);
                    logger.error(e.getMessage(), e);
                }
            }
        }
        mv.addObject(getMessage());
        return mv;
    }

    @RequestMapping(value = "/edit")
    public ModelAndView edit(HttpServletRequest requ, ProductController mc) {

        logger.debug("product.id is " + mc.getProduct().getId());
        product = mc.getProduct();

        if ("get".equalsIgnoreCase(requ.getMethod())) {
            if (!StringUtils.isEmpty(product.getId())) {
                product = productManager.get(product.getId());
            }
        } else {
            product.setUpdateUser(((User) requ.getSession().getAttribute((DataConstant.SESSION_SYS_USER))).getId());
            product.setUpdatetime(getSqlDate());
            try {
                productManager.modify(product);
                product = productManager.get(product.getId());
                addMessage(DataConstant.COMMON_EDIT_SUCCESS);
            } catch (Exception e) {
                addMessage(DataConstant.COMMON_EDIT_FAILED);
                logger.error(e.getMessage(), e);
            }
        }

        ModelAndView mav = new ModelAndView();
        mav.addObject(getMessage());
        mav.addObject(product).setViewName("admin/product/edit");
        return mav;
    }

    @RequestMapping(value = "/remove")
    public ModelAndView remove(HttpServletRequest requ, ProductController mc, Integer pageNo, Integer pageSize) {
        product = mc.getProduct();
        if (null != product && null != product.getId()) {
            product = productManager.get(product.getId());
            product.setUseFlag(DataConstant.COMMON_USE_FLAG_NO);
            try {
                productManager.modify(product);
                addMessage(DataConstant.COMMON_DEL_SUCCESS);
            } catch (Exception e) {
                addMessage(DataConstant.COMMON_DEL_FAILED);
                logger.error(e.getMessage(), e);
            }
        }
        ModelAndView mav = page(requ, mc, pageNo, pageSize);
        mav.addObject(getMessage());
        return mav;
    }

    @RequestMapping(value = "/page")
    public ModelAndView page(HttpServletRequest requ, ProductController ac, Integer pageNo, Integer pageSize) {

        if (null == ac.getProduct()) {
            product = new Product();
        } else {
            product = ac.getProduct();
        }

        if (null == pageNo || null == pageSize) {
            pageNo = 1;
            pageSize = 20;
        }
        logger.debug("productManager is " + productManager);
        product.setUseFlag(DataConstant.COMMON_USE_FLAG_YES);
        IPage<Product> page = productManager.pageSearch(product, pageNo, pageSize);

        ModelAndView mav = new ModelAndView();
        mav.addObject(product);
        mav.addObject(page).setViewName("admin/product/page");
        return mav;
    }

    public ProductCatalogManager getProductCatalogManager() {
        return productCatalogManager;
    }

    public void setProductCatalogManager(ProductCatalogManager productCatalogManager) {
        this.productCatalogManager = productCatalogManager;
    }

    public ProductCatalog getProductCatalog() {
        return productCatalog;
    }

    public void setProductCatalog(ProductCatalog productCatalog) {
        this.productCatalog = productCatalog;
    }

    public ProductManager getProductManager() {
        return productManager;
    }

    public void setProductManager(ProductManager productManager) {
        this.productManager = productManager;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }
}
